<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小同住人员物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\controller\property\repair;

use think\App;
use app\controller\property\Base;
use app\validate\repair\RepairCateValidate;
use app\common\repositories\repair\RepairCateRepository;

class RepairCate extends Base
{
    protected $repository;

    public function __construct(App $app, RepairCateRepository $repository)
    {
        parent::__construct($app);

        $this->repository = $repository;
    }

    public function list()
    {
        if ($this->request->isAjax()) {
            $param = $this->request->param([
                'keywords' => ''
            ]);
            $data = $this->repository->getpropertyList($param,$this->request->propertyId);
            return json()->data(['code' => 0, 'data' => $data['list'], 'count' => $data['count']]);
        }
        return $this->fetch('repair/cate/list', [
            'addAuth' => property_auth('propertyRepairCateAdd'),
            'editAuth' => property_auth('propertyRepairCateEdit'),
            'delAuth' => property_auth('propertyRepairCateDel'),
            'detailAuth' => property_auth('propertyRepairCateDetail')
        ]);
    }


    /**
     * 添加房间
     * @return string|\think\response\Json|\think\response\View
     * @throws \Exception
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param([
                'cate_name' => '',
                'time_limit' => '',
                'picture' => '',
                'pid' => '',
                'is_show' => '',
                'amount' => ''
            ]);
            $param['pid'] = (int)$param['pid'];
            validate(RepairCateValidate::class)->scene('add')->check($param);
            if ($this->repository->cateNameExists($param['cate_name'])) {
                return $this->error('此分类名已存在');
            }
            try {
                $res = $this->repository->addInfo($param,$this->request->propertyId,$this->request->userId);
                if ($res) {
                    return $this->success('添加成功');
                } else {
                    return $this->error('添加失败');
                }
            } catch (\Exception $e) {
                exception_log('添加维修工单分类失败', $e);
                return $this->error('网络失败');
            }
        } else {
            $cateNameData = $this->repository->getCascaderData($this->request->propertyId,['is_show' => 1]);
            return $this->fetch('repair/cate/add', [
                'cateNameData' => $cateNameData
            ]);
        }
    }

    /**
     * 编辑菜单
     * @return array|string|\think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit()
    {
        $id = (int)$this->request->param('id');
        if (!$id) {
            return $this->error('参数错误');
        }
        $info = $this->repository->getDetail($id);
        if (!$info) {
            return $this->error('信息错误');
        }
        if ($this->request->isPost()) {
            $param = $this->request->param([
                'id' => '',
                'pid' => $info['pid'],
                'time_limit' => '',
                'cate_name' => '',
                'picture' => '',
                'amount' => '',
                'is_show' => '',
            ]);
            $param['pid'] = (int)$param['pid'];
            validate(RepairCateValidate::class)->scene('edit')->check($param);
            if ($param['pid'] && $this->repository->getLevelById($param['pid']) > 3) {
                return $this->error('超过最大层级');
            }
            if (!$this->repository->checkChangeToChild($param['id'], $param['pid'])) {
                return $this->error('无法修改到当前分类到子集，请先修改子类');
            }
            if (!$this->repository->checkChildLevel($param['id'], $param['pid'], 4)) {
                return $this->error('子类超过最低限制，请先修改子类');
            }
            try {
                $res = $this->repository->editInfo($info, $param,$this->request->userId);
                if ($res) {
                    return $this->success('修改成功');
                } else {
                    return $this->error('修改失败');
                }
            } catch (\Exception $e) {
                exception_log('编辑维修工单分类失败', $e);
                return $this->error('网络错误');
            }
        } else {
            $cateNameData = $this->repository->getCascaderData($this->request->propertyId,['is_show' => 1]);
            return $this->fetch('repair/cate/edit', [
                'info' => $info,
                'cateNameData' => $cateNameData
            ]);
        }
    }

    public function detail()
    {
        $cateId = (int)$this->request->param('id');
        if ($cateId <= 0) {
            return $this->error('参数错误');
        }
        $info = $this->repository->getDetail($cateId);
        if ($info['id'] <= 0) {
            return $this->error('分类信息不存在');
        }
        return $this->fetch('repair/cate/detail', [
            'info' => $info,
            'editRepairAuth' => property_auth('propertyRepairOrderEdit'),
            'delRepairAuth' => property_auth('propertyRepairOrderDel'),
            'detailRepairAuth' => property_auth('propertyRepairOrderDetail')
        ]);
    }

    public function del()
    {
        validate(RepairCateValidate::class)->scene('del')->check($this->request->param());
        $id = (int)$this->request->param('id');
        $info = $this->repository->getDetail($id);
        if (!$info) {
            return $this->error('信息错误');
        }
        try {
            if ($this->repository->hasChild($id)) {
                return $this->error('该分类存在下级分类，请先处理下级分类');
            }
            $data = $this->repository->delete($id);
            if ($data) {
                return $this->success('删除成功');
            } else {
                return $this->error('删除失败');
            }
        } catch (\Exception $e) {
            exception_log('删除维修工单类失败', $e);
            return $this->error('网络失败');
        }
    }
}